home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SerialPortUtilities.c
-
- Contains: Useful utility functions for dealing with serial ports registered
- with the Communications Resource Manager (CRM).
-
- PC Card modems, PowerBook Internal modems, and NuBus Serial port
- are the most common types of these serial devices.
-
- A single function called:
-
- void ForEachSerialPort(SerialPortActionProcPtr actionProc,void * param);
-
- allows you to record information about available serial ports. Your
- “actionProc” is called once for every serial port in the system and
- is passed the CRMSerialPtr for the corresponding device.
-
- We use this function as a building block for:
-
- void AddSerialPortsToMenu(MenuRef serialPortMenu);
-
- which is very useful for building a popup or other menu from which
- the user can select an available serial port.
-
-
- Once you have a port name (either selected by the user or stored
- in a preferences file), you must get obtain the CRMSerialPtr in
- order to get the driver information and/or icon for a port. For
- this purpose, we include:
-
- CRMSerialPtr GetCRMSerialPtrForPort(StringPtr portName);
- */
-
- #include <CommResources.h>
- #include <CRMSerialDevices.h>
- #include <Errors.h>
- #include <Menus.h>
- #include <TextUtils.h>
-
- #include "SerialPortUtilities.h"
-
-
- void
- ForEachSerialPort(SerialPortActionProcPtr actionProc,void * param)
- {
- CRMRec prototypeCRMRec;
- CRMRecPtr foundCRMRecPtr;
-
- // start searching at the first device
-
- prototypeCRMRec.crmDeviceType = crmSerialDevice;
- prototypeCRMRec.crmDeviceID = 0;
-
- // repeatedly call CRMSearch until we run out of serial devices
-
- while ((foundCRMRecPtr = CRMSearch(&prototypeCRMRec)) != NULL)
- {
- (actionProc)((CRMSerialPtr) foundCRMRecPtr->crmAttributes,param);
-
- prototypeCRMRec.crmDeviceID++; // find the next device
- }
- }
-
-
- // AddSerialPortsToMenu
- //
- // a simple routine which utilizes ForEachSerialPort to add items
-
-
- static void
- AddSerialPortToMenu(CRMSerialPtr serialPortInfo,MenuRef serialPortMenu)
- {
- // Here’s something to watch out for:
- //
- // The StringHandles in a CRMSerialRecord are often unlocked
- // passing an unlocked, dereferenced handle to the Mac Toolbox
- // can be bad, so were careful to ensure we "do the right thing"
-
- SInt8 hState = HGetState((Handle) serialPortInfo->name);
-
- HLock((Handle) serialPortInfo->name);
- AppendMenu(serialPortMenu,*(serialPortInfo->name));
-
- HSetState((Handle) serialPortInfo->name,hState);
- }
-
- void
- AddSerialPortsToMenu(MenuRef serialPortMenu)
- {
- ForEachSerialPort((SerialPortActionProcPtr) AddSerialPortToMenu,NULL);
- }
-
-
- CRMSerialPtr
- GetCRMSerialPtrForPort(StringPtr portName)
- {
- CRMRec prototypeCRMRec;
- CRMRecPtr foundCRMRecPtr;
- CRMSerialPtr serialPortInfo = NULL;
-
- // start searching at the first device
-
- prototypeCRMRec.crmDeviceType = crmSerialDevice;
- prototypeCRMRec.crmDeviceID = 0;
-
- // repeatedly call CRMSearch until we run out of serial devices
-
- while ((foundCRMRecPtr = CRMSearch(&prototypeCRMRec)) != NULL)
- {
- serialPortInfo = (CRMSerialPtr) foundCRMRecPtr->crmAttributes;
-
- if (EqualString(*(serialPortInfo->name), portName, true, true))
- return serialPortInfo;
-
- prototypeCRMRec.crmDeviceID++; // find the next device
- }
-
- return NULL;
- }
-